package com.baomidou.springmvc.task;
import org.springframework.scheduling.Trigger;  
import org.springframework.scheduling.TriggerContext;  
import org.springframework.scheduling.annotation.EnableScheduling;  
import org.springframework.scheduling.annotation.SchedulingConfigurer;  
import org.springframework.scheduling.config.ScheduledTaskRegistrar;  
import org.springframework.scheduling.support.CronTrigger;  
import org.springframework.stereotype.Component;  
  
import java.text.SimpleDateFormat;  
import java.util.Date;  

@Component  
@EnableScheduling 
public class DynamicTask implements SchedulingConfigurer{
	 //时间表达式 ，每天凌晨两点 
    private String cron = " * * 2 * * ?";  
  
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  
    @Override  
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {  
        scheduledTaskRegistrar.addTriggerTask(new Runnable() {  
            @Override  
            public void run() {  
                //任务逻辑  
                System.err.println("---------------start-------------------");  
                System.err.println("当前时间为：" + sdf.format(new Date()));  
                System.err.println("----------------end--------------------");  
            }  
        }, new Trigger() {  
            @Override  
            public Date nextExecutionTime(TriggerContext triggerContext) {  
                CronTrigger cronTrigger = new CronTrigger(cron);  
                Date nextExecDate = cronTrigger.nextExecutionTime(triggerContext);  
                return nextExecDate;  
            }  
        });  
    }

	public String getCron() {
		return cron;
	}

	public void setCron(String cron) {
		this.cron = cron;
	}  
  
  

	
    
    
}
